﻿/*

The MIT License

Copyright (c) 2010 Cartesian Analytics, Inc. 

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

using System;
using System.Collections.Generic;
using System.Linq;
using Pipra.Math;
using Pipra.Math.Geometry;

namespace Pipra.Gis.Transformation
{
    /// <summary>
    /// A molodensky badekas transformation.
    /// </summary>
    public class MolodenskyBadekasTransformation : ITransformation<Point3>
    {

        protected readonly Vector3 D;
        protected readonly Vector3 R;
        protected readonly Point3 O;
        protected readonly double M;

        private class Inverted : InvertedTransformationBase<MolodenskyBadekasTransformation, Point3>
        {

            public Inverted(MolodenskyBadekasTransformation core) : base(core)
            {
                if(0 == Core.M)
                {
                    throw new ArgumentException("Core has no inverse.");
                }
            }

            public override Point3 TransformValue(Point3 coord)
            {
                coord = new Point3(
                    ((coord.X - Core.D.X) / Core.M) - Core.O.X,
                    ((coord.Y - Core.D.Y) / Core.M) - Core.O.Y,
                    ((coord.Z - Core.D.Z) / Core.M) - Core.O.Z
                );
                return new Point3(
                    coord.X + Core.O.X + (coord.Y * Core.R.Z) - (coord.Z * Core.R.Y),
                    coord.Y + Core.O.Y + (coord.Z * Core.R.X) - (coord.X * Core.R.Z),
                    coord.Z + Core.O.Z + (coord.X * Core.R.Y) - (coord.Y * Core.R.X)
                );
            }

        }

        /// <summary>
        /// Constructs a new molodensky badekas transformation.
        /// </summary>
        /// <param name="translationVector">The translation vector.</param>
        /// <param name="rotationVector">The rotation vector.</param>
        /// <param name="rotationOrigin">The origin of rotation.</param>
        /// <param name="scaleFactor">the scaling factor.</param>
        public MolodenskyBadekasTransformation(
            ICoordinateTriple<double> translationVector,
            ICoordinateTriple<double> rotationVector,
            ICoordinateTriple<double> rotationOrigin,
            double scaleFactor
        )
        {
            D = new Vector3(translationVector);
            R = new Vector3(rotationVector);
            O = new Point3(rotationOrigin);
            M = scaleFactor;
        }

        public Point3 TransformValue(Point3 coord)
        {
            coord = new Point3(coord.X - O.X, coord.Y - O.Y, coord.Z - O.Z);
            return new Point3(
                ((coord.X + (coord.Z * R.Y) - (coord.Y * R.Z) + O.X) * M) + D.X,
                ((coord.Y + (coord.X * R.Z) - (coord.Z * R.X) + O.Y) * M) + D.Y,
                ((coord.Z + (coord.Y * R.X) - (coord.X * R.Y) + O.Z) * M) + D.Z
            );
        }

        public bool HasInverse
        {
            get { return 0 != M; }
        }

        public ITransformation<Point3> GetInverse()
        {
            return new Inverted(this);
        }

        ITransformation<Point3, Point3> ITransformation<Point3, Point3>.GetInverse()
        {
            return GetInverse();
        }

        ITransformation ITransformation.GetInverse()
        {
            return GetInverse();
        }

        public IEnumerable<Point3> TransformValues(IEnumerable<Point3> values)
        {
            return values.Select(TransformValue);
        }

        public void TransformValues(Point3[] values)
        {
            for(int i = 0; i < values.Length; i++)
            {
                TransformValue(ref values[i]);
            }
        }

        [CLSCompliant(false)]
        public void TransformValue(ref Point3 value)
        {
            value = TransformValue(value);
        }
    }
}
